package coml.jiyun.hp.day01.fragment;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.day01.R;
import coml.jiyun.hp.day01.adapter.RlvHomeAdapter;
import coml.jiyun.hp.day01.bean.WanBean;
import coml.jiyun.hp.day01.model.ImpWanModel;
import coml.jiyun.hp.day01.presenter.ImpWanPresenter;
import coml.jiyun.hp.day01.view.WanView;

/**
 * A simple {@link Fragment} subclass.
 */

public class HomeFragment extends BaseFragment implements WanView {

    private RecyclerView mRlv;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private RlvHomeAdapter rlvHomeAdapter;
    private ImpWanPresenter impWanPresenter;
    boolean mDataLoaded = false;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        impWanPresenter = new ImpWanPresenter(new ImpWanModel(), this);
        if (!mDataLoaded){
            lazyLoad();
        }
        return inflate;
    }

    private void initView(View inflate) {
        mRlv = inflate.findViewById(R.id.home_rlv);

        datasBeans = new ArrayList<>();
        rlvHomeAdapter = new RlvHomeAdapter(getActivity(), datasBeans);
        mRlv.setAdapter(rlvHomeAdapter);
        mRlv.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onSuccess(WanBean wanBean) {
        datasBeans.addAll(wanBean.getData().getDatas());
        rlvHomeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }

    @Override
    protected void lazyLoad() {
        if (impWanPresenter!=null){
        impWanPresenter.getData();
        mDataLoaded =true;
        }
    }
}